package com.trkj.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Emp;
import com.trkj.service.MyRequestService;
import com.trkj.service.OverWorkService;
import com.trkj.service.StandardService;
import com.trkj.service.WorkFlowService;
/**
 * 我的申请模块的控制器
 * @author Lixd
 *
 */
@Controller
@SessionAttributes({ "emp", "funcIds" })
public class MyRequestController {
	@Autowired
	MyRequestService myRequestService;
	@Autowired
	WorkFlowService workFlowService;
	@Autowired
	OverWorkService overWorkService;
	@Autowired
	StandardService standardService;
	/**
	 * 删除暂存的流程
	 * @param processId
	 * @param processName
	 * @return
	 */
	@RequestMapping("/deleteProcess")
	@ResponseBody
	public boolean deleteProcess(Integer processId,String processName) {
		return myRequestService.deleteProcess(processId,processName);
	}
	/**
	 * 提交暂存的流程
	 * @param model
	 * @param processId
	 * @param processDefinitionKey
	 * @return
	 */
	@RequestMapping("/submit_readyemploys")
	@ResponseBody
	public boolean submit_readyemploys(Model model, Integer processId,
			@RequestParam("pk") String processDefinitionKey) {
		Integer useid = getOnLineEmp(model);
		if (processId != null && processId > 0) {
			if ("overwork_act".equals(processDefinitionKey)) {
				return overWorkService.submit_overwork(useid, processId, processDefinitionKey);
			}else if ("standard_act".equals(processDefinitionKey)){
				return standardService.isStandard(useid);
			}
			return workFlowService.startEmployProcess(processId, useid, processDefinitionKey);
		}
		return false;

	}

	/**
	 * 获取登录人的id
	 * 
	 * @author Lixd
	 * @param model
	 * @return
	 */
	private Integer getOnLineEmp(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer useid = emp.getEmpId();
		return useid;
	}
	/**
	 * 我的申请，分页条件查询入口
	 * @param model
	 * @param my_processID
	 * @param my_processName
	 * @param my_processState
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/find_my_processes")
	@ResponseBody
	public Map<String, Object> find_my_processes(Model model, String my_processID, String my_processName,
			String my_processState, Integer page, Integer rows) {
		Emp emp = (Emp) model.asMap().get("emp");
		Map<String, Object> map = myRequestService.getPageMyRequest(emp.getEmpId(), my_processID, my_processState,
				my_processName, page, rows);
		return map;
	}
}
